<?php
/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

class IndexController extends Zend_Controller_Action 
{
	/**
	 * The default action - show the home page
	 */
    public function indexAction() 
    {
    	// instantiate the user repository who will be in charge of building a proper user
        $userRepo = new user_UserRepository();
        
        // find a user with id 1 in the DB and grab their current nickname from the profile
        $user = $userRepo->find(1);
        $this->view->nickname = $user->Profile->getNickname();
        
		// set their nickname in their profile to something new
        $user->Profile->setNickname('tommy tom');
        
        // find the user again, it should pull the same object reference and not create a brand new object (Identity Map)
        $user = $userRepo->find(1);
        $this->view->nickname2 = $user->Profile->getNickname(); 
        
       
     
        
        // a fictional web service to check to see how popular your new nickname is
        $profileService = new services_ProfileService();
        $popularity = $profileService->howCommonIsMyNickname($user);
        $this->view->popularity = $popularity;
        
        
        // let's update the user's email address
        $user->setEmailAddress('newdudeyahoo.com');
        
        // you can manually commit the save to the database like this or in the destruct method of ObjectManager 
        // it will do it automatically for you, this will save all the changes we've made to all our objects
        // using the unit of work pattern in ObjectManager, the profile and the user will get updated
       	$this->view->errors = ObjectManager::performOperations();

        
       
    }
    
    public function updateAction()
    {
    	$this->view->errors = false;
    	
    	// instantiate the user repository who will be in charge of building a proper user
		$userRepo = new user_UserRepository();
		
    	$user = $userRepo->find(1);
    	
    	// pass the form the validator it needs to work with
    	$form = new forms_ChangeEmailForm(array('Validator' => new user_validation_Default($user)));
    	

		// find a user with id 1 in the DB
    	if ($this->getRequest()->isPost()) {
	            if($form->isValid($_POST)) {
	   
	    		$values = $form->getValues();

		        // let's update the user's email address to what was posted from the form
		        $user->setEmailAddress($values['email']);
		        
		        // update the profile's nickname
		        $user->Profile->setNickname($values['nickname']);
        
		        
		    	// check to make sure the whole user object is valid with these new changes
		        if(!$user->isValid()) {
		        	$this->view->errors = $user->getMessages();
		        } else {
		        	$userRepo->save($user);
		        }
	    	}
    	
        }
        
        $this->view->email = $user->email;
        $this->view->nickname = $user->Profile->getNickname();
    	
    	$this->view->form = $form;
    	
    	

    }
    
    /**
     * this controller action will aim to show how to create a user
     *
     */
    public function createuserAction()
    {
    	// we're going to be creating a user
    	$userRepo = new user_UserRepository();
    	$user = $userRepo->getNewUser();
    	
    	// pass the form the validator it needs to work with
    	$form = new forms_CreateUserForm(array('Validator' => new user_validation_Default($user)));
    	

		// find a user with id 1 in the DB
    	if ($this->getRequest()->isPost()) {
	            if($form->isValid($_POST)) {
	   
	    		$values = $form->getValues();

		        // let's update the user's email address to what was posted from the form
		        $user->setEmailAddress($values['email']);
		        
		        // update the profile's nickname
		        $user->Profile->setNickname($values['nickname']);
        
		        
		    	// check to make sure the whole user object is valid with these new changes
		        if(!$user->isValid()) {
		        	$this->view->errors = $user->getMessages();
		        } else {
		        	$userRepo->save($user);
		        	$this->view->message = "USER CREATED!";
		        }
	    	}
    	
        }
    	
    	$this->view->form = $form;
    	
    }
    
    
    
 	public function goodemailAction()
    {
    	 	// instantiate the user repository who will be in charge of building a proper user
        $userRepo = new user_UserRepository();
        
        // find a user with id 1 in the DB and grab their current nickname from the profile
        $user = $userRepo->find(1);
        
        // let's update the user's email address to something that should work
        $user->setEmailAddress('validuser@yahoo.com');
        
       
        // check to make sure the user is valid before we save them to the DB
        if(!$user->isValid()) {
        	$this->view->errors = $user->getMessages();
        } else {
        	$userRepo->save($user);
        	$this->view->errors = false;
        }

    }
    
    
   
    
}
